<template>
  <div>
    <el-form :inline="true" class="filter-blk flex-1">
      <el-form-item label="设备编号">
        <el-input
          placeholder="请输入设备编号"
          v-model.trim="filterForm.number"
        ></el-input>
      </el-form-item>
      <el-form-item label="安装位置">
        <el-input
          placeholder="请输入安装位置"
          v-model.trim="filterForm.position"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchAction"
          ><i class="iconfont icon-search"></i> 查询</el-button
        >
        <el-button type="primary" @click="getMarkersData">确定选择</el-button>
      </el-form-item>
    </el-form>
    <el-table
      ref="multipleTableRef"
      :data="dataList"
      style="width: 100%"
      v-loading="loading"
      @selection-change="handleSelectionChange"
      row-key="id"
    >
      <el-table-column :reserve-selection="true" type="selection" width="55" />
      <el-table-column
        type="index"
        label="序号"
        width="90"
        :index="indexMethod"
      ></el-table-column>
      <template v-if="isAdmin && setting">
        <el-table-column
          prop="name"
          label="企业名称"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="areaName"
          label="所属行政区域"
          align="center"
        ></el-table-column>
      </template>
      <template v-else>
        <el-table-column prop="number" label="设备编号"></el-table-column>
        <el-table-column prop="name" label="设备名称"></el-table-column>
        <el-table-column
          prop="deviceTypeName"
          label="设备类型"
        ></el-table-column>
        <el-table-column prop="position" label="安装位置"></el-table-column>
      </template>

      <!-- <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button type="primary" @click="selectAction(scope.row)"
            >选择</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>
    <el-pagination
      v-if="total > 0"
      class="text-right mt-20 mr-20"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="pageSize"
      layout="prev, pager, next, total, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>
<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      filterForm: {},
      loading: false,
      total: 0,
      currentPage: 1,
      pageSize: 10,
      loading: false,
      dataList: [],
      markersData: [],
      multipleTableRef: null,
    };
  },
  created() {
    this.getData(1);
  },
  mounted() {},
  computed: {
    ...mapState(["siteInfos"]),
    isAdmin() {
      return this.siteInfos && this.siteInfos.userType == 1;
    },
  },
  props: {
    businessId: {
      type: String,
      default: "",
    },
    coordinateData: {
      type: Array,
      default: [],
    },
    setting: {
      type: Boolean,
      default: false,
    },
  },
  methods: {
    //部分显示，默认回显 id 为 1 的勾选框
    toggleSelection(rows) {
      if (rows) {
        let result = rows.filter((o) =>
          this.coordinateData.some((v) => {
            if (!this.isAdmin && this.setting) {
              return o.number == v.deviceId;
            } else {
              return o.id == v.deviceId;
            }
          })
        );
        result.forEach((row) => {
          this.$refs.multipleTableRef.toggleRowSelection(row, true); // 默认勾选
        });
      } else {
        this.$refs.multipleTableRef.clearSelection();
      }
    },
    getData(page) {
      this.loading = true;
      let url;
      let params;
      // if (this.businessId) {
      //   params.businessId = this.businessId;
      // }
      if (this.isAdmin && this.setting) {
        url = "/business/business/businessListPage";
        params = {
          page: page,
          limit: this.pageSize,
          area: "",
          sort: false,
        };
      } else {
        url = "/fireFightingIOT/device/deviceListPage";
        params = Object.assign(
          {
            page: page,
            limit: this.pageSize,
          },
          this.filterForm
        );
      }

      this.$get(url, params).then(
        (res) => {
          this.loading = false;
          let data = res.data;
          if (data) {
            this.currentPage = page;
            this.total = data.totalSize;
            this.dataList = data.Items;
            this.toggleSelection(this.dataList);
          }
        },
        (err) => {
          this.loading = false;
          this.currentPage = page;
          this.total = 0;
          this.dataList = [];
          if (err.msg) {
            this.$message.error(err.msg);
          }
        }
      );
    },

    indexMethod(index) {
      return (this.currentPage - 1) * this.pageSize + index + 1;
    },
    searchAction() {
      this.getData(1);
    },
    handleCurrentChange(page) {
      this.getData(page);
    },
    selectAction(item) {
      let type = this.coordinateData.find((sitem) => sitem.deviceId == item.id);
      console.log(type);
      if (type) {
        this.$message.error("请勿重复添加设备！");
        return;
      }
      this.$emit("select", item);
    },
    handleSelectionChange(val) {
      this.markersData = val;
    },
    getMarkersData() {
      console.log(this.markersData);
      this.$emit("select", this.markersData);
    },
  },
};
</script>
